<?php
/**
 * Display a Frame
 */
use Nawa\MedicalStudy\MedicalCenter\Monitoring;
use Fox\Core\Errors;
use Fox\Html\Input;
use Fox\Generator;
use Fox\Html\Html;
use Fox\Html\Button;
use Fox\Popup;
use Fox\Msg;
use Fox\Dao\SqlFragment;
use Fox\Document\Type;
use Fox\Html\Input\File;
use Fox\Html\DropList;
use Fox\Html\Block;
use Fox\Html\Screen;

$xview = Xview::instance();
$screen = new Screen('sDocument','import');
$block = new Block('bDocument');
// Doc Type
$finder = new Type();
$types = $finder->find(new SqlFragment("category = ?",array(Monitoring::DOC_CATEGORY)));
$block->add(new DropList('doctype',false,false,true))->options($types,'doctype','description');
$block->add(new File('filename',true));
$block->add(new Input('inlineid',true,$xview->val('inlineid')))->hidden();
$block->noLegend();
$screen->add($block);
if($screen->submitted())
{
	$fields = $screen->get();
	if(!$screen->errors())
	{
		try {
			$object = Fox\Dao::load(Monitoring::className(),$fields['inlineid']);
			$fileName = \Xview::moveFileToServer();
			$data['filename'] = $fileName['filename'];
			$data['destination'] = _TEMP_;
			$data['language'] = Xview::instance()->language();
			$data['doctype'] = $fields['doctype'];
			$data['physicalname'] = $fileName['physicalname'];
			// Create Document Object
			$document = new \Fox\Document();
			$document->setFields($data);
			$document->create();
			$resData['docid'] = $document->getId();
			$resData['uname'] = User::current()->getId();
			$resData['udate'] = \Fox\Document::sqlDateTime(time());
			$documentLink = new Monitoring\Document();
			$documentLink->msid = $object->msid;
			$documentLink->mcid = $object->mcid;
			$documentLink->monitoringid = $object->monitoringid;
			$documentLink->setFields($resData);
			$object->replaceDocuments($documentLink);
			$object->update();
		}
		catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
	}
}
$popup = new Popup('pAddDocument',Msg(Msg::BUNDLE,Msg::TITLE_ADD_DOCUMENT),$screen);
$caller = $xview->val('caller','MC_gMonitorings');
$screen->ajaxUpload(Generator::ajaxReload($caller));
echo $popup->display();